using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class asp_pages_ShoppingCart : System.Web.UI.Page
{
    List<Item> list;
    Item item = new Item();
    PetDAO daoPet = new PetDAO();
    ProductDAO daoProduct = new ProductDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            if (Request["id"] == null) Response.Redirect("Home.aspx");
            int id = int.Parse(Request["id"]);
            int typeId = int.Parse(Request["type_id"]);
            if (typeId == 1)
            {
                Pet pet = daoPet.GetPet(id);
                item.ID = pet.ID;
                item.TypeID = pet.TypeID;
                item.Name = pet.Name;
                item.Price = pet.Price;
                item.Quantity = 1;
            }
            else if (typeId == 0)
            {
                Product product = daoProduct.GetProduct(id);
                item.ID = product.ID;
                item.TypeID = product.TypeID;
                item.Name = product.Name;
                item.Price = product.Price;
                item.Quantity = 1;
            }
            Session["curItem"] = item;
            lblItemName.Text = item.Name;
            lblPrice.Text = item.Price.ToString();
            txtQuantity.Text = "1";
           
            LoadData();
            btnAccept.Focus();
        }
    }
    public void LoadData()
    {
        list = ((List<Item>)Session["cart"]);
        grvShoppingCart.DataSource = list;
        grvShoppingCart.DataBind();
    }
    protected void btnAccept_Click(object sender, EventArgs e)
    {
        
        Item curItem = Session["curItem"] as Item;
        if (curItem != null)
        {
            curItem.Quantity = int.Parse(txtQuantity.Text);
            list = (List<Item>)Session["cart"];
            bool existed = false;
            foreach (Item it in list)
            {
                if (it.ID == curItem.ID && it.TypeID==curItem.TypeID)
                {
                    it.Quantity = curItem.Quantity;
                    existed = true;
                    break;
                }
            }
            if(!existed)list.Add(curItem);
            Session["cart"] = list;
            LoadData();
            btnAccept.Enabled = false;
            pnlAdd.Visible = false;
        }
        
    }
    protected void grvShoppingCart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvShoppingCart.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void grvShoppingCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvShoppingCart.EditIndex = -1;
        LoadData();
    }
    protected void grvShoppingCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
}
